<cfcomponent output="false">
	<cfset $ = application.helpers />
	<cfset keys = ['F','U','N','D','R','2','4','6','7','9'] />
	
	<!---  --->
	
	<cffunction name="saveFund" access="public" output="true" returntype="string">
		<cfargument name="fundID" required="false" default="" />
		<cfargument name="title" required="true" />
		<cfargument name="who" required="true" />
		<cfargument name="description" required="true" />
		<cfargument name="end_date" required="false" default="" />
		<cfargument name="target" required="false" default="" />
		<cfargument name="private" required="false" default="0" />
	
		<cfset var local = {} />

		<cftransaction isolation="serializable">
			<!--- check arguments --->
			
			<cfif len(session.user.id) neq 32>
				<cfreturn -1 />
			</cfif>
			
			<cfif arguments.target eq "">
				<cfset arguments.target = 0 />
			</cfif>
			
			<cfset local.userID = session.user.id />
			<cfif arguments.title eq "" or arguments.who eq "" or arguments.description eq "">
				<cfthrow message="Missing required data." />
			</cfif>
			<!--- <cfif arguments.end_date neq ''>
				<cfset arguments.end_date=$.format.date(arguments.end_date) />
			</cfif> --->
			
			<cfset local.fundID = $.database.save(
				table="fund",
				id=arguments.fundID,
				user_id=session.user.id,
				title=arguments.title,
				who=arguments.who,
				description=arguments.description,
				end_date=arguments.end_date,
				private=arguments.private,
				amount=arguments.target
			) />
			
			<cfif arguments.fundID eq "">
				<cfset checkFundKeyPool() />
				<cfset local.key = getFundKey() />
				<cfset $.database.save(
					table="fund_key",
					id=local.key.id,
					fund_id=local.fundID
				) />
			</cfif>
			
		</cftransaction>
		
		<cfreturn local.fundID />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getFunds" access="public" output="true" returntype="query">
	
		<cfreturn application.g.fundGateway.getFunds(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="generateKeys" access="public" output="true" returntype="void">
		<cfargument name="number" required="false" default="100" />
	
		<cfset var local = {} />
				
		<cfloop from="1" to="#arguments.number#" index="local.i">
			<cfset local.key = generateKey() />

			<cfif len(local.key) eq 5>
				<cfset $.database.save(
					table="fund_key",
					id="",
					fund_key=local.key
				) />
			</cfif>
		</cfloop>
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="generateKey" access="private" output="true" returntype="string">
	
		<cfset var local = {} />
		
		<cfset local.key = "" />
		
		<cfloop from="1" to="5" index="local.i">
			<cfset local.key = local.key & keys[randrange(1,10)] />
		</cfloop>
		
		<cfset local.testKey = testFundKey(key=local.key) />
		
		<cfif local.testKey.recordcount gt 0>
			<cfreturn "" />
		</cfif>
		
		<cfreturn local.key />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="testFundKey" access="public" output="true" returntype="query">
	
		<cfreturn application.g.fundGateway.testFundKey(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="checkFundKeyPool" access="public" output="true" returntype="void">
		
		<cfset var local = {} />
	
		<cfquery name="local.checkFundKeyPool" datasource="fundrdb">
			select count(id) as counts
			from fund_key
			where fund_key.fund_id is null
		</cfquery>
		
		<cfif local.checkFundKeyPool.counts lt 50>
			<cfset generateKeys() />
		</cfif>
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getFundKey" access="public" output="true" returntype="query">
		
		<cfset var local = {} />
		
		<cfquery name="local.getFundKey" datasource="fundrdb">
			select 
			hex(id) as id
			,fund_key
			from fund_key
			where active = 1
			and fund_id is null
			limit 1
		</cfquery>
		
		<cfreturn local.getFundKey />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getIDByFundID" access="public" output="true" returntype="query">
		<cfargument name="fundID" required="true" />
	
		<cfreturn application.g.fundGateway.getIDByFundID(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getPolicy" access="public" output="true" returntype="struct">
	
		<cfreturn $.format.s3signature() />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getContacts" access="public" output="true" returntype="query">
		<cfargument name="fundID" required="true" />
		<cfargument name="sort" required="false" default="created">
	
		<cfreturn application.g.fundGateway.getContacts(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveContact" access="public" output="true" returntype="struct">
		<cfargument name="contactID" required="false" default="" />
		<cfargument name="name" required="false" default="no name" />
		<cfargument name="email" required="true" />
		<cfargument name="fundID" required="true" />
	
		<cfset var local = {} />
		
		<cfset sleep(1500) />
		
		<cfset local.fund = getIDByFundID(arguments.fundID) />
				
		<cfset local.contactID = $.database.save(
			table="fund_contact",
			id=arguments.contactID,
			fund_id=local.fund.id,
			name=arguments.name,
			email=$.format.encrypt(arguments.email)
		) />
		
		<cfset local.result = {} />
		<cfset local.result.id = local.contactID />
		<cfset local.result.email = trim(arguments.email) />
		<cfset local.result.name = trim(arguments.name) />
		
		<cfreturn local.result />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="deleteContacts" access="public" output="true" returntype="string">
		<cfargument name="contactIDs" required="true" />
	
		<cfset var local = {} />
		
		<cfset sleep(500) />
		
		<cfif arguments.contactIDs neq ''>
			<cfquery name="local.deleteContacts" datasource="fundrdb">
				update fund_contact
				set active = 0
				where id in #$.database.uuids(arguments.contactIDs)#
			</cfquery>
		</cfif>
		
		<cfreturn true />
		
	</cffunction>
	
	<!--- --->
	
</cfcomponent>